package com.sixbro.caicaitv.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.sixbro.caicaitv.BuildConfig;
import com.sixbro.caicaitv.R;
import com.sixbro.caicaitv.bean.ChannelBean;
import com.sixbro.caicaitv.common.Consts;
import com.sixbro.caicaitv.util.ToastUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WelcomeActivity extends AppCompatActivity {

    private String strContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        init();
    }

    private void init() {
        loadData();
    }

    private void loadData() {
        String url = "https://gitee.com/huagua/caicaitv/raw/master/creator/live.txt";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtils.show("源数据获取异常,请检查您的网络设置");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finish();
                            }
                        }, 2000);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                strContent = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (TextUtils.isEmpty(strContent) || !strContent.contains("[")) {
                            ToastUtils.show("源数据获取异常");
                            finish();
                            return;
                        }
                        strContent = strContent.substring(strContent.indexOf("["), strContent.lastIndexOf("]") + 1);
                        Type type = new TypeToken<List<ChannelBean>>() {
                        }.getType();
                        List<ChannelBean> list;
                        try {
                            list = new Gson().fromJson(strContent, type);
                        } catch (JsonSyntaxException e) {
                            ToastUtils.show("源数据获取异常");
                            finish();
                            return;
                        }
                        if (BuildConfig.DEBUG) {
                            Consts.DEFAULT_CHANNELS.addAll(list);
                        } else {
                            Consts.DEFAULT_CHANNELS = list;
                        }
                        startActivity(new Intent(WelcomeActivity.this, TvActivity.class));
                        finish();
                    }
                });
            }
        });
    }

}